import cv2
import numpy as np
cap=cv2.VideoCapture(r"D:\Project\Project37\Project37\tupian\zhuangjiaban.mp4")
b=cv2.createBackgroundSubtractorMOG2()
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
while True:
    ret,frame=cap.read()
    print(frame.shape)
    if ret:
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        ret1,gray=cv2.threshold(gray,120,255,cv2.THRESH_BINARY)
        blur=cv2.GaussianBlur(gray,(3,3),5)
        erode=cv2.erode(blur,kernel,3)
        dilate=cv2.dilate(erode,kernel,5)
        open=cv2.morphologyEx(dilate,cv2.MORPH_OPEN,kernel)
        mask=b.apply(open)
        cnt,h=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        for (i,c) in enumerate(cnt):
            x,y,w,h=cv2.boundingRect(c)
            y1=y+h/2;x1=x+w/2
            if y1<=50 or x1<=485:
                continue
           
            cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
        cv2.imshow('frame',frame)
    
    key=cv2.waitKey(25)
    
    if key==27:
        break
cap.release()
cv2.destroyAllWindows()